/*
 * Copyright (C) 2009 Internal Innovation Technology(IIT)
 * 
 */
package com.iitbd.hxcalc;

import java.io.*;
import java.util.Locale;
import java.util.Properties;
import org.apache.log4j.Logger;

/**
 * This class is used for default configuration purpose
 *
 * @author Prasun Kanti Howlader <prasun@iitbd.com>
 */
public class AppConfig implements AppProperties {

    private static Logger logger = Logger.getLogger(AppConfig.class.getName());
    private Properties m_propsconfig;
    private File configfile;

    public AppConfig() {
        init(getDefaultConfig());
    }

    public AppConfig(File configfile) {
        init(configfile);
    }

    private void init(File configfile) {
        this.configfile = configfile;
        m_propsconfig = new Properties();

        logger.info("Reading configuration file: " + configfile.getAbsolutePath());
    }

    private File getDefaultConfig() {
        return new File(new File(System.getProperty("user.dir")), File.separatorChar + "conf" + File.separatorChar + AppDefaults.APP_NAME + ".properties");
    }

    public String getProperty(String sKey) {
        return m_propsconfig.getProperty(sKey);
    }

    public File getConfigFile() {
        return configfile;
    }

    public void setProperty(String sKey, String sValue) {
        if (sValue == null) {
            m_propsconfig.remove(sKey);
        } else {
            m_propsconfig.setProperty(sKey, sValue);
        }
    }

    /**
     * Delete the config file
     *
     */
    public boolean delete() {
        return configfile.delete();
    }

    public void load() {

        try {
            InputStream in = new FileInputStream(configfile);
            if (in != null) {
                m_propsconfig = new Properties();
                m_propsconfig.load(in);
                in.close();
            }
        } catch (IOException e) {
            loadDefault();
        }

    }

    /**
     * Save the configuration file
     */
    public void save() throws IOException {
        OutputStream out = new FileOutputStream(configfile);
        if (out != null) {
            m_propsconfig.store(out, AppDefaults.APP_NAME + ".properties");
            out.close();
            logger.info("Save configuration file: " + configfile.getAbsolutePath());
        }
    }

    /**
     * Loading Factory default Settings
     */
    private void loadDefault() {
        m_propsconfig = new Properties();
        Locale l = new Locale("en", "US");
        m_propsconfig.setProperty("user.language", l.getLanguage());
        m_propsconfig.setProperty("user.country", l.getCountry());
        m_propsconfig.setProperty("defultmantisse", "8");
        m_propsconfig.setProperty("defaultendnotaion", "off");
        m_propsconfig.setProperty("defultmtemp", "K");
        m_propsconfig.setProperty("defultenthalphy", "J/g");
        m_propsconfig.setProperty("defultdensity", "g/m3");
        m_propsconfig.setProperty("defultpressure", "pa");
     
        logger.info("Loading Factory default Setting");

    }
}
